# 备份管理脚本
# 用途: 管理项目备份，包括查看、清理和比较备份

param(
    [switch]$ListBackups = $false,      # 列出所有备份
    [switch]$ListDetails = $false,      # 列出备份详细信息
    [switch]$Cleanup = $false,          # 清理旧备份
    [int]$KeepCount = 5,                # 保留最新备份的数量
    [string]$Compare1 = "",             # 第一个备份目录
    [string]$Compare2 = "",             # 第二个备份目录
    [switch]$Help = $false              # 显示帮助信息
)

# 项目路径
$ProjectDir = "d:\src\DotNet\demo\demo"

# 显示帮助信息
function Show-Help {
    Write-Host "备份管理脚本使用说明:" -ForegroundColor Green
    Write-Host ""
    Write-Host "用法:" -ForegroundColor Yellow
    Write-Host "  .\BackupManager.ps1 -ListBackups           # 列出所有备份"
    Write-Host "  .\BackupManager.ps1 -ListDetails           # 列出备份详细信息"
    Write-Host "  .\BackupManager.ps1 -Cleanup [-KeepCount N] # 清理旧备份，保留N个最新备份(默认5个)"
    Write-Host "  .\BackupManager.ps1 -Compare1 <备份1> -Compare2 <备份2> # 比较两个备份"
    Write-Host "  .\BackupManager.ps1 -Help                  # 显示帮助信息"
    Write-Host ""
}

# 列出所有备份
function List-Backups {
    $backupDirs = Get-ChildItem -Directory -Path $ProjectDir -Name "Backup_*" | Sort-Object -Descending
    
    if ($backupDirs.Count -eq 0) {
        Write-Host "没有找到任何备份目录。" -ForegroundColor Red
        return
    }
    
    Write-Host "找到 $($backupDirs.Count) 个备份目录:" -ForegroundColor Green
    Write-Host ""
    
    for ($i = 0; $i -lt $backupDirs.Count; $i++) {
        $dir = $backupDirs[$i]
        $dirInfo = Get-Item (Join-Path $ProjectDir $dir)
        $date = $dirInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")
        
        Write-Host "[$($i+1)] $dir (创建时间: $date)" -ForegroundColor Cyan
    }
}

# 列出备份详细信息
function List-BackupDetails {
    $backupDirs = Get-ChildItem -Directory -Path $ProjectDir -Name "Backup_*" | Sort-Object -Descending
    
    if ($backupDirs.Count -eq 0) {
        Write-Host "没有找到任何备份目录。" -ForegroundColor Red
        return
    }
    
    Write-Host "备份详细信息:" -ForegroundColor Green
    Write-Host ""
    
    for ($i = 0; $i -lt $backupDirs.Count; $i++) {
        $dir = $backupDirs[$i]
        $dirInfo = Get-Item (Join-Path $ProjectDir $dir)
        $date = $dirInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")
        
        Write-Host "备份 [$($i+1)]: $dir" -ForegroundColor Cyan
        Write-Host "  创建时间: $date"
        
        # 检查备份文件
        $files = @("Form1.cs", "Form1.Designer.cs", "Form1.resx")
        Write-Host "  包含文件:"
        
        foreach ($file in $files) {
            $filePath = Join-Path $ProjectDir $dir $file
            if (Test-Path $filePath -PathType Leaf) {
                $fileInfo = Get-Item $filePath
                $size = [math]::Round($fileInfo.Length / 1KB, 2)
                Write-Host "    - $file (大小: $size KB)"
            } else {
                Write-Host "    - $file (缺失)" -ForegroundColor Red
            }
        }
        
        Write-Host ""
    }
}

# 清理旧备份
function Cleanup-Backups {
    param([int]$KeepCount)
    
    $backupDirs = Get-ChildItem -Directory -Path $ProjectDir -Name "Backup_*" | Sort-Object -Descending
    
    if ($backupDirs.Count -le $KeepCount) {
        Write-Host "当前有 $($backupDirs.Count) 个备份，不需要清理。" -ForegroundColor Green
        return
    }
    
    $dirsToDelete = $backupDirs | Select-Object -Skip $KeepCount
    
    Write-Host "将删除以下 $($dirsToDelete.Count) 个旧备份:" -ForegroundColor Yellow
    $dirsToDelete | ForEach-Object { Write-Host "  - $_" }
    
    $confirm = Read-Host "确认删除吗? (y/N)"
    
    if ($confirm -eq "y" -or $confirm -eq "Y") {
        foreach ($dir in $dirsToDelete) {
            $dirPath = Join-Path $ProjectDir $dir
            Remove-Item $dirPath -Recurse -Force
            Write-Host "已删除: $dir" -ForegroundColor Green
        }
        Write-Host "备份清理完成!" -ForegroundColor Green
    } else {
        Write-Host "已取消备份清理操作。" -ForegroundColor Yellow
    }
}

# 比较两个备份
function Compare-Backups {
    param([string]$Backup1, [string]$Backup2)
    
    $backup1Path = Join-Path $ProjectDir $Backup1
    $backup2Path = Join-Path $ProjectDir $Backup2
    
    # 检查备份目录是否存在
    if (-not (Test-Path $backup1Path -PathType Container)) {
        Write-Host "错误: 备份目录 '$Backup1' 不存在!" -ForegroundColor Red
        return
    }
    
    if (-not (Test-Path $backup2Path -PathType Container)) {
        Write-Host "错误: 备份目录 '$Backup2' 不存在!" -ForegroundColor Red
        return
    }
    
    Write-Host "比较备份: $Backup1 vs $Backup2" -ForegroundColor Green
    Write-Host ""
    
    $files = @("Form1.cs", "Form1.Designer.cs", "Form1.resx")
    
    foreach ($file in $files) {
        $file1Path = Join-Path $backup1Path $file
        $file2Path = Join-Path $backup2Path $file
        
        Write-Host "比较文件: $file" -ForegroundColor Cyan
        
        if (-not (Test-Path $file1Path -PathType Leaf)) {
            Write-Host "  $Backup1 中缺少此文件" -ForegroundColor Red
            continue
        }
        
        if (-not (Test-Path $file2Path -PathType Leaf)) {
            Write-Host "  $Backup2 中缺少此文件" -ForegroundColor Red
            continue
        }
        
        # 比较文件内容
        $diff = Compare-Object (Get-Content $file1Path) (Get-Content $file2Path)
        
        if ($diff.Count -eq 0) {
            Write-Host "  文件内容相同" -ForegroundColor Green
        } else {
            Write-Host "  文件内容不同，差异行数: $($diff.Count)" -ForegroundColor Yellow
            
            # 显示前几行差异
            $diff | Select-Object -First 5 | ForEach-Object {
                if ($_.SideIndicator -eq "<=") {
                    Write-Host "    - $Backup1: $($_.InputObject)" -ForegroundColor Gray
                } else {
                    Write-Host "    + $Backup2: $($_.InputObject)" -ForegroundColor Gray
                }
            }
            
            if ($diff.Count -gt 5) {
                Write-Host "    ... 还有 $($diff.Count - 5) 行差异" -ForegroundColor Gray
            }
        }
        
        Write-Host ""
    }
}

# 主逻辑
if ($Help) {
    Show-Help
    exit
}

if ($ListBackups) {
    List-Backups
    exit
}

if ($ListDetails) {
    List-BackupDetails
    exit
}

if ($Cleanup) {
    Cleanup-Backups -KeepCount $KeepCount
    exit
}

if (-not [string]::IsNullOrEmpty($Compare1) -and -not [string]::IsNullOrEmpty($Compare2)) {
    Compare-Backups -Backup1 $Compare1 -Backup2 $Compare2
    exit
}

# 如果没有指定任何操作，显示帮助
Write-Host "请指定要执行的操作。" -ForegroundColor Yellow
Show-Help